<template>
  <div>
    <el-cascader-panel
      v-model="obj.a"
      :options="pca"
      :props="{ label: 'name', value: 'name' }"
      @change="findData"
    ></el-cascader-panel>
    <div style="margin-top: 10px">
      <div style="width: 50%; float: left">
        <el-form
          ref="form"
          :rules="rules"
          :model="obj"
          label-width="80px"
          class="demo-ruleForm"
        >
          <el-form-item label="确诊" prop="confirmed">
            <el-input v-model="obj.confirmed"></el-input>
          </el-form-item>
        </el-form>

        <el-form
          ref="form"
          :model="obj"
          label-width="80px"
          :rules="rules"
          class="demo-ruleForm"
        >
          <el-form-item label="疑似" prop="suspected">
            <el-input v-model="obj.suspected"></el-input>
          </el-form-item>
        </el-form>

        <el-form
          ref="form"
          :model="obj"
          label-width="80px"
          :rules="rules"
          class="demo-ruleForm"
        >
          <el-form-item label="死亡" prop="dead">
            <el-input v-model="obj.dead"></el-input>
          </el-form-item>
        </el-form>

        <el-form
          ref="form"
          :model="obj"
          label-width="80px"
          :rules="rules"
          class="demo-ruleForm"
        >
          <el-form-item label="治愈" prop="cure">
            <el-input v-model="obj.cure"></el-input>
          </el-form-item>
        </el-form>

        <el-form
          ref="form"
          :model="obj"
          label-width="80px"
          :rules="rules"
          class="demo-ruleForm"
        >
          <el-form-item label="重症" prop="severe">
            <el-input v-model="obj.severe"></el-input>
          </el-form-item>
        </el-form>

        <el-form
          ref="form"
          :model="obj"
          label-width="80px"
          :rules="rules"
          class="demo-ruleForm"
        >
          <el-form-item label="境外输入" prop="outside">
            <el-input v-model="obj.outside"></el-input>
          </el-form-item>
        </el-form>
        <el-form ref="form" :model="obj" label-width="80px">
          <el-button @click="save">提交</el-button>
        </el-form>
      </div>
      <div style="width: 47%; float: right; color: red; font-weight: 800">
        <div>温馨提示:</div>
        <div>1.先选择城市</div>
        <div>2.再录入数据</div>
      </div>
    </div>
  </div>
</template>

<script>
import pca from "../../assets/pca.json";
import { mapActions, mapState } from "vuex";
import moment from "moment";

export default {
  data() {
    var checkConfirmed = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("确诊人数不能为空"));
      }
      setTimeout(() => {
        if (!Number.isInteger(Number(value))) {
          callback(new Error("请输入数字值"));
        }
      }, 1000);
    };
    var checkSuspected = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("疑似人数不能为空"));
      }
      setTimeout(() => {
        if (!Number.isInteger(Number(value))) {
          callback(new Error("请输入数字值"));
        }
      }, 1000);
    };
    var checkDead = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("死亡人数不能为空"));
      }
      setTimeout(() => {
        if (!Number.isInteger(Number(value))) {
          callback(new Error("请输入数字值"));
        }
      }, 1000);
    };
    var checkCure = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("治愈人数不能为空"));
      }
      setTimeout(() => {
        if (!Number.isInteger(Number(value))) {
          callback(new Error("请输入数字值"));
        }
      }, 1000);
    };
    var checkSevere = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("重症人数不能为空"));
      }
      setTimeout(() => {
        if (!Number.isInteger(Number(value))) {
          callback(new Error("请输入数字值"));
        }
      }, 1000);
    };
    var checkOutside = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("境外输入人数不能为空"));
      }
      setTimeout(() => {
        if (!Number.isInteger(Number(value))) {
          callback(new Error("请输入数字值"));
        }
      }, 1000);
    };
    return {
      pca,
      obj: {
        country: "中国",
      },
      a2: [],
      rules: {
        confirmed: [{ validator: checkConfirmed, trigger: "blur" }],
        suspected: [{ validator: checkSuspected, trigger: "blur" }],
        dead: [{ validator: checkDead, trigger: "blur" }],
        cure: [{ validator: checkCure, trigger: "blur" }],
        severe: [{ validator: checkSevere, trigger: "blur" }],
        outside: [{ validator: checkOutside, trigger: "blur" }],
      },
    };
  },
  computed: {
    ...mapState("dataUpdata", ["dataList"]),
  },
  methods: {
    ...mapActions("dataUpdata", ["saveOrupdata", "page_query"]),
    //数据上报
    save() {
      if (this.obj.a) {
        this.obj.province = this.obj.a[0];
        this.obj.city = this.obj.a[1];
        this.obj.area = this.obj.a[2];
        delete this.obj.a;
      }
      this.saveOrupdata(this.obj).then((res) => {
       
        this.$router.push({
          path: "/data_List/table",
        });
      });
    },
    //查询某地区当天是否录入数据
    findData(val) {
      this.a2 = this.obj.a;
      //val[2]
      //moment(new Date()).format('YYYY-MM-DD')
      let params = {
        page: 1,
        pageSize: 1,
        area: val[2],
        inputTime: moment(new Date()).format("YYYY-MM-DD"),
      };
      this.page_query(params).then((res) => {
        if (this.dataList.total > 0) {
          this.obj = this.dataList.list[0];
          this.obj.a = [
            this.dataList.list[0].province,
            this.dataList.list[0].city,
            this.dataList.list[0].area,
          ];
        } else {
          this.obj = {};
          this.obj.a = this.a2;
          this.obj.country = "中国";
        }
      });
    },
  },
};
</script>